package com.zeus.base.smscenter.controller;

import com.zeus.base.privilege.model.TbCompanyRoleMenuAuthority;
import com.zeus.base.privilege.model.TbUser;
import com.zeus.base.privilege.service.ICompanyRoleMenuAuthorityService;
import com.zeus.base.privilege.service.IUserService;
import com.zeus.base.privilege.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * Created by ShanFei on 2017-03-04.
 * 获取Shiro权限控制器
 */
@RestController
@RequestMapping("/controller/shiroPermission")
public class ShiroPermissionController {
    @Autowired
    private ICompanyRoleMenuAuthorityService iCompanyRoleMenuAuthorityService;
    /**
     * 获取权限,返回JSON格式权限字符串
     */
    @RequestMapping(value="/getRolePermission",method = RequestMethod.POST)
    @ResponseBody
    public String[] getRolePermission(@RequestParam(name = "menuId" ,required=false)  Integer menuId,@RequestParam(name = "menuCode" ,required=false)  String menuCode,HttpServletRequest request){
        TbUser user=(TbUser)request.getSession().getAttribute("sessionUser");
        HashMap<String,Object> params = new HashMap<String,Object>();
        params.put("companyRoleIds", StringUtil.startsWithAndEndsWith(user.getRoleIds()));
        params.put("companyIds", StringUtil.startsWithAndEndsWith(user.getCompanyIds()));
        if(StringUtil.isNotEmpty(menuCode))params.put("menuCode",menuCode);
        List<TbCompanyRoleMenuAuthority> roleMenuAuthorityList = iCompanyRoleMenuAuthorityService.findListRoleMenuAuthorityByParams(params);
        String[] permissionArr = null;
        permissionArr = new String[roleMenuAuthorityList.size()];
        for (int i=0;i<roleMenuAuthorityList.size();i++){
            TbCompanyRoleMenuAuthority t = roleMenuAuthorityList.get(i);
            permissionArr[i] = t.getProductLineMenu().getMenuCode()+":"+t.getProductLineAuthority().getProductLineAuthorityCode();
        }
        return permissionArr;
    }
}
